package com.wjjun.test.ui

import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.wjjun.app.BR
import com.wjjun.base.ui.activity.BaseListActivity
import com.wjjun.base.ui.viewmodel.BaseItemViewModel
import com.wjjun.base.ui.viewmodel.BaseViewModel
import com.wjjun.app.R
import com.wjjun.base.ui.listener.IItemViewLongClickListener
import com.wjjun.test.ui.itemviewmodel.Test2ItemViewModel
import com.wjjun.test.ui.itemviewmodel.TestItemViewModel
import com.wjjun.test.ui.viewmodel.TestListViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import me.tatarka.bindingcollectionadapter2.ItemBinding
import kotlin.collections.ArrayList

/**
 * 列表activity
 * author : wangjunjun
 * date   : 2022/6/29
 * desc   :
 */
@AndroidEntryPoint
class TestListActivity : BaseListActivity<TestListViewModel, ViewDataBinding>(), IItemViewLongClickListener{

    override fun initContent() {
        setTitle("测试列表界面")
        setRefreshEnable(true)
        setLoadMoreEnable(true)
    }

    override fun initParams() {
    }

    override suspend fun fetchData() {
        //测试动画
        delay(1000)
        refreshData()
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.tv_add -> {
                if (System.currentTimeMillis() % 2 == 0L) {
                    addItem(TestItemViewModel("随机1"))
                } else {
                    addItem(Test2ItemViewModel("随机2"))
                }
            }
            R.id.tv_clear -> {
                clearData()
            }
        }
    }

    override suspend fun fetchData(pageNum: Int, pageSize: Int, useCache: Boolean, param: Any?) {
        val list = ArrayList<BaseItemViewModel<*>>()
        list.add(TestItemViewModel("自动获取pageNum:${pageNum}"))
        list.add(TestItemViewModel("自动获取${pageNum}"))
        setData(list, pageNum)
    }

    override fun onItemClick(view: View, itemViewModel: BaseViewModel) {
        when (itemViewModel) {
            is TestItemViewModel -> {
                when (view.id) {
                    R.id.tv_edit -> {
                        itemViewModel.editData {
                            it?.name = "编辑1后${System.currentTimeMillis()}"
                        }
                    }
                    R.id.tv_delete -> {
                        removeItem(itemViewModel)
                    }
                }
            }
            is Test2ItemViewModel -> {
                when (view.id) {
                    R.id.tv_edit -> {
                        itemViewModel.editData {
                            it?.name ="编辑2后${System.currentTimeMillis()}"
                        }
                    }
                    R.id.tv_delete -> {
                        removeItem(itemViewModel)
                    }
                }
            }
        }
    }

    override fun  onListItemBind(itemBinding: ItemBinding<*>,
                                 position: Int,
                                 itemViewModel: BaseItemViewModel<*>){
        itemBinding.bindExtra(BR.longListener, this)
    }

    override fun onItemLongClick( view: View, viewModel: BaseViewModel): Boolean {
        Toast.makeText(this,"长按Item", Toast.LENGTH_SHORT).show()
        return true
    }
}